//
//  shareViewController.m
//  ILiangCang
//
//  Created by ZX on 16-4-11.
//  Copyright (c) 2016年 CRQ. All rights reserved.
//

#import "shareViewController.h"
#import "shareLeftViewController.h"
#import "shareMainCell.h"
#import "shareModel.h"
#import "httpEngine.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"



#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface shareViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong)NSMutableArray * dataArray;
@property (nonatomic,strong)UIView * navView;
@property (nonatomic,strong)UICollectionView * collectionView;
@property(nonatomic,assign)NSInteger currentPage;
@end

@implementation shareViewController

-(void)viewWillAppear:(BOOL)animated
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBarHidden = YES;
    _dataArray = [[NSMutableArray alloc]initWithCapacity:0];
    [self addNavView];
    [self addCollectionView];
    self.currentPage = 1;
    
    
}
-(void)addNavView
{
    _navView = [[UIView alloc]init];
    _navView.frame = CGRectMake(0, 0, WIDTH, 64);
    [self.view addSubview:_navView];
    UIImageView * navBG = [[UIImageView alloc]init];
    navBG.frame = CGRectMake(0, 0, WIDTH, 64);
    navBG.image = [UIImage imageNamed:@"nav_bar"];
    [_navView addSubview:navBG];
    UIButton * menuBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    menuBtn.frame = CGRectMake(10, 24, 34, 34);
    menuBtn.tag = 1;
    [menuBtn setBackgroundImage:[UIImage imageNamed:@"nav_op"] forState:UIControlStateNormal];
    [menuBtn addTarget:self action:@selector(menuBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_navView addSubview:menuBtn];

}

-(void)menuBtnClick:(UIButton *)sender
{
    sender.tag++;
    if (sender.tag % 2 == 0)
    {
        [self.drawer open];
    }else
    {
        [self.drawer close];
    }
}

-(void)addCollectionView
{
     UICollectionViewFlowLayout * layout = [UICollectionViewFlowLayout new];
    layout.sectionInset = UIEdgeInsetsMake(10, 2, 10, 2);
    layout.minimumLineSpacing = 8;
    layout.itemSize = CGSizeMake(WIDTH/2-8, WIDTH/2-14);
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT-64-49) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor colorWithRed:50.0/255 green:50.0/255 blue:50.0/255 alpha:1];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [self.view addSubview:_collectionView];
    
    [_collectionView registerClass:[shareMainCell class] forCellWithReuseIdentifier:@"cell"];
    
    [_collectionView addHeaderWithTarget:self action:@selector(headerRefreshing)];
    [_collectionView addFooterWithTarget:self action:@selector(footerRefreshing)];
    
    [httpEngine requestToGetShareMainWith:1 Completion:^(NSMutableArray *shareArray, NSError *error) {
        _dataArray = shareArray;
        [_collectionView reloadData];
    }];
    
    
    
}

#pragma mark----上下拉刷新表的方法
-(void)headerRefreshing
{
    self.currentPage=1;
    [httpEngine requestToGetShareMainWith:1 Completion:^(NSMutableArray *shareArray, NSError *error) {
        _dataArray = shareArray;
        [_collectionView reloadData];

    }];
    [self.collectionView headerEndRefreshing];
}

-(void)footerRefreshing
{
    self.currentPage++;
    [httpEngine requestToGetShareMainWith:self.currentPage Completion:^(NSMutableArray *shareArray, NSError *error) {
        
        if (error)
        {
            return ;
        }
        if (shareArray.count>0)
        {
            for (int i=0; i<shareArray.count; i++)
            {
                [_dataArray addObject:shareArray[i]];
                [_collectionView reloadData];
            }

        }else
        {
            NSLog(@"没了");
        }
        
    }];
    
    [self.collectionView footerEndRefreshing];
    
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    shareMainCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    shareModel * share = _dataArray[indexPath.item];
    [cell.bg sd_setImageWithURL:share.goodsImage
    completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        NSStringFromCGSize(image.size);
        if (image.size.width >= image.size.height) {
            CGRect rect = cell.frame;
            rect.size.width = cell.frame.size.width;
            rect.size.height = cell.frame.size.width *image.size.height/image.size.width;
            cell.bg.frame = CGRectMake(0, (cell.frame.size.height-rect.size.height)/2, rect.size.width, rect.size.height);
            
        }else {
            CGRect rect = cell.frame;
            rect.size.height = cell.frame.size.height;
            rect.size.width = cell.frame.size.height *image.size.width/image.size.height;
            cell.bg.frame = CGRectMake((cell.frame.size.width-rect.size.width)/2, 0, rect.size.width, rect.size.height);
        }
        
    }];
    return cell;
    
}
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    if(velocity.y>0)
        
    {
        [UIView animateWithDuration:0.5 animations:^{
            _navView.frame =CGRectMake(0, -64, WIDTH, 64);
            _collectionView.frame = CGRectMake(0, 20, WIDTH, HEIGHT-20);
            [_collectionView reloadData];
            [self hideTabBar:YES];
        } completion:nil];
        
    }else
    {
        
        [UIView animateWithDuration:0.5 animations:^{
            _navView.frame =CGRectMake(0, 0, WIDTH, 64);
            _collectionView.frame = CGRectMake(0, 64, WIDTH, HEIGHT-64-49);
            [_collectionView reloadData];
            [self hideTabBar:NO];
        } completion:nil];
        
    }
    
}


- (void) hideTabBar:(BOOL) hidden{
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.4];
    
    for(UIView *view in self.tabBarController.view.subviews)
    {
        if([view isKindOfClass:[UITabBar class]])
        {
            if (hidden) {
                [view setFrame:CGRectMake(0, HEIGHT, WIDTH, view.frame.size.height)];
                view.alpha = 0;
                
            } else {
                [view setFrame:CGRectMake(0, HEIGHT-49, WIDTH, view.frame.size.height)];
                view.alpha = 1;
            }
        }
        else
        {
            if (hidden) {
                [view setFrame:CGRectMake(0, view.frame.origin.y, view.frame.size.width, view.frame.size.height)];
                
            } else {
                [view setFrame:CGRectMake(0, view.frame.origin.y, WIDTH,  HEIGHT-49)];
                
            }
        }
    }
    
    [UIView commitAnimations];
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
